package com.zz.controller;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.URLEncoder;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Deliverylist;
import com.zz.entity.DeliverylistExcel;
import com.zz.entity.Entry;
import com.zz.entity.Product;
import com.zz.service.DeliverylistService;
import com.zz.service.EntryService;
import com.zz.util.KeyUtil;

@RestController
@RequestMapping("deliverylist")
public class DeliverylistController {
	@Resource
	DeliverylistService deliverylistService;
	@Resource
	EntryService entryService;
	
	//保存出库信息
		@RequestMapping("savedeliverylist")
			public Map saveEntry(HttpServletRequest request) {
				String productid=request.getParameter("outproductid");
				String productname=request.getParameter("outname");
				String producttype=request.getParameter("outtype");
				
				String productnum=request.getParameter("outnumid");
				String productlocation=request.getParameter("outlocationid");
				String wareid=request.getParameter("outwareidin");
				String supplierid=request.getParameter("outsupplieridin");
				String entrynotes=request.getParameter("outnotesid");
				int num=entryService.findproductById(productid).get().getNum();
				BigDecimal productprice=entryService.findproductById(productid).get().getPrice();
				Product product=new Product();
				product.setId(productid);
				product.setName(productname);
				product.setType(producttype);
				product.setPrice(productprice);
				product.setNum((num-Integer.parseInt(productnum)));
				System.out.println("产品库存："+product.getNum());
				product.setLocation(productlocation);
				product.setWareId(wareid);
				product.setSupplierId(supplierid);
				deliverylistService.saveproduct(product);
				
				Deliverylist deliverylist=new Deliverylist();
				deliverylist.setId("CUK"+KeyUtil.genUniqueKey());
				deliverylist.setProductId(productid);
				deliverylist.setNotes(entrynotes);
				deliverylist.setSupplierId(supplierid);
				deliverylist.setOutNum(Integer.parseInt(productnum));;
				deliverylist.setWareId(wareid);
				deliverylistService.savedeliverylist(deliverylist);
				Map map=new HashMap();
				map.put("productentityCUK", product);
				map.put("deliverylistentity",deliverylist);
				return map;
			}
		//对出库单进行分页查询
		@RequestMapping("showAllDeliverylist")
		public Page<Object[]> showAllDeliverylist(HttpServletRequest request){
			String startpage=request.getParameter("start");
			System.out.println("showAllEntryliststartpage=="+startpage);
			return deliverylistService.showAllDeliverylist(startpage, "10");
		}
		//出库单表最近一周查询
		@RequestMapping("showDeliverylistBy7day")
		public Page<Object[]> showDeliverylistBy7day(HttpServletRequest request){
			String startpage=request.getParameter("start");
			System.out.println("showAllEntryliststartpage=="+startpage);
			return deliverylistService.showDeliverylistBy7day(startpage, "5");
		}
		//出库单表最近一周查询
			@RequestMapping("showDeliverylistBy30day")
			public Page<Object[]> showDeliverylistBy30day(HttpServletRequest request){
				String startpage=request.getParameter("start");
				System.out.println("showAllEntryliststartpage=="+startpage);
				return deliverylistService.showDeliverylistBy30day(startpage, "5");
			}
		//出库单表一段时间查询
			@RequestMapping("showDeliverylistBytime")
			public Page<Object[]> showDeliverylistBytime(HttpServletRequest request){
				String starttime=request.getParameter("selectstarttime");
				String lasttime=request.getParameter("selectlasttime");
				System.out.println("starttime------"+starttime);
				System.out.println("lasttime-------"+lasttime);
				String startpage=request.getParameter("start");
				System.out.println("showAllEntryliststartpage=="+startpage);
				return deliverylistService.showDeliverylistBytime(starttime, lasttime, startpage, "5");
			}
		//导出出库excel表
			@RequestMapping(value = "UserExcelDownloads", method = RequestMethod.GET)
			public void getexcel(HttpServletRequest request,HttpServletResponse response){
//				String starttime=request.getParameter("excelbtnstarttime");
//				String lasttime=request.getParameter("excelbtnlasttime");
//				System.out.println("excelstarttime------"+starttime);
//				System.out.println("excellasttime-------"+lasttime);
				
				String fileName = "出库单表.xlsx";//设置要导出的文件的名字
		        //新增数据行，并且设置单元格数据

				 XSSFWorkbook wk = new XSSFWorkbook();
				 XSSFSheet sheet = wk.createSheet("全部出库单");
				 ArrayList<DeliverylistExcel> deliverylist=deliverylistService.showAllDeliverylist2();
				 
				 
				 // 表头样式
			        XSSFFont xssfFont = wk.createFont();
			        // 加粗
			        xssfFont.setBold(true);
			        // 字体名称
			        xssfFont.setFontName("宋体");
			        // 字体大小
			        xssfFont.setFontHeight(12);
			        XSSFCellStyle headStyle = wk.createCellStyle();
			        // 设置字体css
			        headStyle.setFont(xssfFont);
			        // 竖向居中
			        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			        // 横向居中
			        headStyle.setAlignment(HorizontalAlignment.CENTER);
			        // 边框
			        //headStyle.setBorderBottom(BorderStyle.THIN);
			        headStyle.setBorderLeft(BorderStyle.THIN);
			        headStyle.setBorderRight(BorderStyle.THIN);
			        headStyle.setBorderTop(BorderStyle.THIN);
			        String[] headers = { "出库单编号", "商品名称", "出库数量", "出库时间","供应商","仓库","备注"};
			        XSSFRow row1 = sheet.createRow(0);
			        for (int i=0;i<headers.length;i++) {
			        	XSSFCell row1Cell1 = row1.createCell(i);
			        	XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			        	row1Cell1.setCellValue(text);
			        	row1Cell1.setCellStyle(headStyle);
			        }
				 
			     // 内容字体样式
			        XSSFFont contFont = wk.createFont();
			        // 加粗
			        contFont.setBold(false);
			        // 字体名称
			        contFont.setFontName("宋体");
			        // 字体大小
			        contFont.setFontHeight(11);
			        // 内容样式
			        XSSFCellStyle contentStyle = wk.createCellStyle();
			        // 设置字体css
			        contentStyle.setFont(contFont);
			        // 竖向居中
			        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			        // 横向居中
			        //contentStyle.setAlignment(HorizontalAlignment.CENTER);
			        // 边框
			        contentStyle.setBorderBottom(BorderStyle.THIN);
			        contentStyle.setBorderLeft(BorderStyle.THIN);
			        contentStyle.setBorderRight(BorderStyle.THIN);
			        contentStyle.setBorderTop(BorderStyle.THIN);
			        // 自动换行
			        contentStyle.setWrapText(true);
			       
			        
			        int rowNum = 1;
			        //在表中存放查询到的数据放入对应的列
			        for (DeliverylistExcel deliverylistExcel:deliverylist) {
			            XSSFRow row2 = sheet.createRow(rowNum);
			            row2.createCell(0).setCellValue(deliverylistExcel.getDelId());
			            row2.createCell(1).setCellValue(deliverylistExcel.getProductname());
			            row2.createCell(2).setCellValue(deliverylistExcel.getDelOut_num());
			            SimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
			            row2.createCell(3).setCellValue(time.format(deliverylistExcel.getDelOut_time()));
			            row2.createCell(4).setCellValue(deliverylistExcel.getGysname());
			            row2.createCell(5).setCellValue(deliverylistExcel.getCkname());
			            row2.createCell(6).setCellValue(deliverylistExcel.getDelNotes());
			            rowNum++;
			        }

			        Charset charset = Charset.forName("UTF-8");

			        response.setContentType("application/octet-stream;charset=utf-8");
			        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.DEFAULT.encode(fileName, charset));
			        try {
						response.flushBuffer();
						wk.write(response.getOutputStream());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			        
			
			}
}
